# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer:
pkgname=elogind
pkgver=246.10
pkgrel=4
pkgdesc="Standalone fork of systemd's elogind"
url="https://github.com/elogind/elogind"
arch="all"
license="GPL-2.0-or-later LGPL-2.1-or-later"
depends="dbus shadow"
options="!check" # Tests fail on builders
makedepends="
	meson
	coreutils
	docbook-xsl
	gettext-dev
	git
	gperf
	intltool
	libxslt-dev
	glib-dev
	acl-dev
	eudev-dev
	libcap-dev
	libseccomp-dev
	linux-pam-dev
	m4
	dbus-dev
	pcre2-dev
	"
subpackages="
	$pkgname-dev
	$pkgname-doc
	$pkgname-lang
	$pkgname-openrc
	lib$pkgname:libs
	$pkgname-zsh-completion:zshcomp:noarch
	$pkgname-bash-completion:bashcomp:noarch
	"
source="https://github.com/elogind/elogind/archive/v$pkgver/elogind-v$pkgver.tar.gz
	elogind.initd
	fix-version.patch
	remove-polkit-gobject-1-check.patch
	"

build() {
	case "$CARCH" in
		# polkit is not available on these arches
		s390x|riscv64) _polkit=false; export CFLAGS="$CFLAGS -D__IGNORE_pkey_mprotect -DSO_PEERSEC=31" ;;
		*) _polkit=true ;;
	esac
	export LDFLAGS="$LDFLAGS -lintl"
	abuild-meson \
		-Dcgroup-controller=elogind \
		-Dhalt-path=/sbin/halt \
		-Drootlibexecdir=/usr/libexec/elogind \
		-Dreboot-path=/sbin/reboot \
		-Ddefault-hierarchy=hybrid \
		-Ddefault-kill-user-processes=false \
		-Dpolkit=$_polkit \
		-Dman=true \
		build
	meson compile ${JOBS:+-j ${JOBS}} -C build
}

check() {
	meson test --no-rebuild -v -C build
}

package() {
	DESTDIR="$pkgdir" meson install --no-rebuild -C build

	# Claim compatiblity with systemd and systemd-logind
	ln -s libelogind.pc "$pkgdir"/usr/lib/pkgconfig/libsystemd.pc
	ln -s libelogind.pc "$pkgdir"/usr/lib/pkgconfig/libsystemd-logind.pc
	ln -s /usr/include/elogind "$pkgdir"/usr/include/systemd

	# Install headers from elogind
	install -Dm644 src/systemd/sd-id128.h usr/include/sd-id128.h
	install -Dm644 src/systemd/_sd-common.h usr/include/_sd-common.h

	# Install openrc service
	install -Dm755 "$srcdir"/elogind.initd "$pkgdir"/etc/init.d/elogind
}

zshcomp() {
	pkgdesc="Zsh completion for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel zsh"

	mkdir -p "$subpkgdir"/usr/share
	mv "$pkgdir"/usr/share/zsh "$subpkgdir"/usr/share/
}

bashcomp() {
	pkgdesc="Bash completion for $pkgname"
	install_if="$pkgname=$pkgver-r$pkgrel bash-completion"

	mkdir -p "$subpkgdir"/usr/share
	mv "$pkgdir"/usr/share/bash-completion "$subpkgdir"/usr/share
}

sha512sums="
9db0f068ed94ec07bab4d764ccb38840af3d05a4b7c9c539721906f5381b509cb9a3cbfb0453a978210d306136368de6162578c600d522416ef2a7ac1b9f348b  elogind-v246.10.tar.gz
e73738488e78af099fdbb7c93a411a3e8d69080a93f89c245cb8c6de5fe4cb7fe724687d19e21eb6be52ccc795ec200d9c6e499efb1afd2b27467f7f541a7dd1  elogind.initd
178c33608f0d69fde8558c521aea9981e0a0bd4a6666d2e1b4f0714dbb7038955ceb86cf323ea741ee67f89218a2e7d332aa05507c2a06eb01fd0785dd81716b  fix-version.patch
5cf63f50e8623a4a6e403924860ba76b5dcd19220514c382175afb6f07dbbccc97a27f858ae0c7bc6869f7558255fffc3174e3ac468e5d47333d9cf1db9a5ac1  remove-polkit-gobject-1-check.patch
"
